﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumTime.Models;

namespace ScrumTime.Services
{
    public class UsabilityFeatureService
    {
        ScrumTimeEntities _ScrumTimeEntities;

        public UsabilityFeatureService(ScrumTimeEntities scrumTimeEntities)
        {
            _ScrumTimeEntities = scrumTimeEntities;
        }

        public static UsabilityFeature GetUsabilityFeatureById(ScrumTimeEntities scrumTimeEntities, int id)
        {
            UsabilityFeature usabilityFeature = null;
            var results = from t in scrumTimeEntities.UsabilityFeatures
                          where t.UsabilityFeatureId == id
                          select t;
            if (results.Count() > 0)
                usabilityFeature = results.First<UsabilityFeature>();
            else
               usabilityFeature = new UsabilityFeature();
            return usabilityFeature;
        }

        public UsabilityFeature GetUsabilityFeatureById(int id)
        {
            return GetUsabilityFeatureById(_ScrumTimeEntities, id);
        }

        public UsabilityFeature SaveUsabilityFeature(UsabilityFeature usabilityFeature)
        {
            if (usabilityFeature != null)
            {
                if (usabilityFeature.UsabilityFeatureId == 0)  // this is new
                {
                    _ScrumTimeEntities.AddToUsabilityFeatures(usabilityFeature);
                }
                else  // the UsabilityFeature exists
                {
                    _ScrumTimeEntities.AttachTo("UsabilityFeatures", usabilityFeature);

                    ScrumTimeEntities freshScrumTimeEntities =
                        new ScrumTimeEntities(_ScrumTimeEntities.Connection.ConnectionString);
                    UsabilityFeature existingUsabilityFeature = GetUsabilityFeatureById(freshScrumTimeEntities, usabilityFeature.UsabilityFeatureId);
                    if (existingUsabilityFeature == null)
                    {
                        throw new Exception("The UsabilityFeature no longer exists.");
                    }
                    _ScrumTimeEntities.ObjectStateManager.ChangeObjectState(usabilityFeature, System.Data.EntityState.Modified);
                }
                _ScrumTimeEntities.SaveChanges();
            }
            return usabilityFeature;
        }

        public void DeleteUsabilityFeature(int usabilityFeatureId)
        {
            UsabilityFeature existingUsabilityFeature = GetUsabilityFeatureById(usabilityFeatureId);

            if (existingUsabilityFeature != null && existingUsabilityFeature.UsabilityFeatureId > 0)
            {
                _ScrumTimeEntities.DeleteObject(existingUsabilityFeature);
                _ScrumTimeEntities.SaveChanges();
            }
            else
                throw new Exception("You have attempted to delete a UsabilityFeature that does not exist.");
        }

        // TODO: Ultimately remove GetUsabilityFeatureIdOfSample() once no sample is needed.
        public int GetUsabilityFeatureIdOfSample()
        {
            int usabilityFeatureIdOfSample = -1;
            var results = from p in _ScrumTimeEntities.UsabilityFeatures
                          where p.Name.StartsWith("Sample -")
                          select p.UsabilityFeatureId;
            if (results.Count() > 0)
                usabilityFeatureIdOfSample = (int)results.First();

            return usabilityFeatureIdOfSample;
        }


        //--------------codigo insertado

        // Ob
        public static List<UsabilityFeature> GetAllUsabilityFeatures(ScrumTimeEntities scrumTimeEntities)
        {
            var results = from s in scrumTimeEntities.UsabilityFeatures                          
                          orderby s.Name ascending
                          select s;
            return results.ToList<UsabilityFeature>();
        }

        //-------------- fin de codigo insertado
    }
}